package com.topvision.jack.camerasdk2.camera.callback;


import android.content.Context;
import android.hardware.Camera;
import android.os.Build;


import com.topvision.jack.camerasdk2.camera.utils.FileUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/*
拍照回传方法
*@Params :
*@Author : gushenjie
*@Date :17/12/23
*/
public class TakePhotoCallback implements Camera.PictureCallback {

    Context context;
    String path;

    public TakePhotoCallback(Context context,String path){
        this.context = context;
        this.path = path;
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        try {
            String folderPath = FileUtils.getFolderName(path);
            FileUtils.createFolder(context,folderPath);
            File dstFile = new File(path);
            saveToSDCard(context,data,dstFile);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 将拍下来的照片存放在SD卡中
     *
     * @param data
     * @throws IOException
     */
    public static File saveToSDCard(Context context, byte[] data, File file) throws IOException {
        //将byte数组写入到某个文件当中,
        FileOutputStream outputStream = new FileOutputStream(file); // 文件输出流
        outputStream.write(data); // 写入sd卡中
        outputStream.close(); // 关闭输出流
        return file;
    }
}
